﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;


public class MyApplicationEventsClass : IHttpModule
{
    #region IHttpModule Members

    public void Dispose()
    {
       
    }

    public void Init(HttpApplication context)
    {
        //HttpContext.Current.Trace.Write("start init");
        //context.Error += new EventHandler(context_Error);
                
        //SessionStateModule sm = (SessionStateModule)context.Modules["Session"];
        //sm.Start += new EventHandler(sm_Start);
        //sm.End += new EventHandler(sm_End);
       

        


    }

    void sm_End(object sender, EventArgs e)
    {
        HttpContext.Current.Trace.Write("session end");
        HttpApplicationState Application = HttpContext.Current.Application;
        try
        {


            Application.Lock();

            Application["CurrentCount"] = long.Parse(Application["CurrentCount"].ToString()) - 1;

            Application.UnLock();
        }
        catch
        {
        }

        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.
    }

    void sm_Start(object sender, EventArgs e)
    {


        HttpContext.Current.Trace.Write("session start");
        HttpApplicationState Application = HttpContext.Current.Application;


        if (Application["Iam Started"] == null)
            StartApp();
        
        try
        {
            DatabaseComponent.HistoryVisitsDB objHistoryVisitsDB = new DatabaseComponent.HistoryVisitsDB(ConnectionStrings.LMS);

            DatabaseComponent.HistoryVisits objHistoryVisits = new DatabaseComponent.HistoryVisits();
            objHistoryVisits.NoOfMembers = DatabaseComponent.HistoryVisits.MyCount + 1;

            objHistoryVisitsDB.Insert(objHistoryVisits);

            Application.Lock();

            Application["CurrentCount"] = long.Parse(Application["CurrentCount"].ToString()) + 1;

            long lngNew = (long)Application["CurrentCount"];
            DateTime dtCurrent = DateTime.Now;

            Application.UnLock();


            if (lngNew > DatabaseComponent.MaxVisitsDate.objMax.MaxConcurrenceNumber)
            {
                DatabaseComponent.MaxVisitsDateDB objMaxDB = new DatabaseComponent.MaxVisitsDateDB(ConnectionStrings.LMS);
                DatabaseComponent.MaxVisitsDate obj = new DatabaseComponent.MaxVisitsDate();
                obj.MaxConcurrenceNumber = lngNew;
                obj.CurrentDate = dtCurrent;

                objMaxDB.Insert(obj);
            }


        }
        catch
        {
        }

        // Code that runs when a new session is started

    }

    private void StartApp()
    {
        //Application Start


        HttpContext.Current.Trace.Write("application start");


        HttpServerUtility Server = HttpContext.Current.Server;
        HttpApplicationState Application = HttpContext.Current.Application;


        Application.Lock();

        Application["Iam Started"] = true;

        Application.UnLock();

        System.Collections.Generic.List<FileGarbishCollection> lstNew = new System.Collections.Generic.List<FileGarbishCollection>();
        lstNew.Add(new FileGarbishCollection(ConnectionStrings.LMS, "Courses", "Path", Server.MapPath("./CoursesFiles")));
        lstNew.Add(new FileGarbishCollection(ConnectionStrings.LMS, "Courses", "Path", Server.MapPath("./CourseFiles")));
        //FileGarbishCollection.startClearThread(lstNew);


        // Code that runs on application startup
        Application["CurrentCount"] = 0;

        //try
        //{
        //    Application["emailData"] = GlobalVars.getEmailData();
        //}
        //catch
        //{
        //}
    }

    void context_Error(object sender, EventArgs e)
    {
        try
        {
            HttpContext.Current.Trace.Write("error");



            HttpServerUtility Server = HttpContext.Current.Server;

            Exception ex = Server.GetLastError();

            if (!ex.Message.Equals("File does not exist."))
            {

                DatabaseComponent.LmsErrorsDB objLmsErrorDB = new DatabaseComponent.LmsErrorsDB(ConnectionStrings.LMS);
                DatabaseComponent.LmsErrors objLmsError = new DatabaseComponent.LmsErrors();
                if (ex.Data != null)
                    objLmsError.ErrData = ex.Data.ToString();
                objLmsError.ErrMessage = ex.Message;
                if (sender != null)
                    objLmsError.ErrSender = sender.ToString();
                objLmsError.ErrStackTrace = ex.StackTrace;
                objLmsError.ErrToString = ex.ToString();

                objLmsErrorDB.Insert(objLmsError);
            }



        }
        catch
        {
        }
    }

    #endregion
}